package com.ruoyi.web.controller.vqms;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.vqms.domain.VQMSFactory;
import com.ruoyi.vqms.domain.VQMSStorehouse;
import com.ruoyi.vqms.service.IVQMSFactoryService;
import com.ruoyi.vqms.service.IVQMSStorehouseLocationService;
import com.ruoyi.vqms.service.IVQMSStorehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 仓库 控制器
 */
@RestController
@RequestMapping("/vqms/storehouse")
public class VQMSStorehouseController extends BaseController {
    @Autowired
    private IVQMSStorehouseService storehouseService;
    @Autowired
    private IVQMSStorehouseLocationService storehouseLocationService;

    /**
     * 查询仓库列表(不分页)
     */
    @PreAuthorize("@ss.hasPermi('vqms:storehouse:list')")
    @RequestMapping("/getList")
    public AjaxResult getList(VQMSStorehouse vqmsStorehouse){
        return AjaxResult.success(storehouseService.selectList(vqmsStorehouse));
    }

    /**
     * 查询仓库列表
     */
    @PreAuthorize("@ss.hasPermi('vqms:storehouse:list')")
    @RequestMapping("/list")
    public TableDataInfo list(VQMSStorehouse vqmsStorehouse){
        startPage();
        List<VQMSStorehouse> list=storehouseService.selectList(vqmsStorehouse);
        return getDataTable(list);
    }

    /**
     * 新增仓库
     */
    @Log(title = "新增仓库", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('vqms:storehouse:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody VQMSStorehouse vqmsStorehouse){
        vqmsStorehouse.setCreateBy(SecurityUtils.getUsername());
        storehouseService.add(vqmsStorehouse);
        return AjaxResult.success();
    }

    /**
     * 查询仓库
     */
    @PreAuthorize("@ss.hasPermi('vqms:storehouse:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(storehouseService.getById(id));
    }

    /**
     * 修改仓库
     */
    @Log(title = "修改仓库", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('vqms:storehouse:update')")
    @PutMapping(value = "/update")
    public AjaxResult update(@RequestBody VQMSStorehouse vqmsStorehouse)
    {
        vqmsStorehouse.setUpdateBy(SecurityUtils.getUsername());
        storehouseService.update(vqmsStorehouse);
        return AjaxResult.success();
    }

    /**
     * 删除仓库
     */
    @Log(title = "删除仓库", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('vqms:storehouse:delete')")
    @DeleteMapping(value = "/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        for(Long storehouseId : ids){
            int num=storehouseLocationService.countByStorehouseId(storehouseId);
            if(num>0){
                return AjaxResult.error("删除失败，请先删除该仓库的库位！");
            }
        }
        storehouseService.delete(ids);
        return AjaxResult.success();
    }

}
